<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python高级 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>SQL演练 | python高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day16/dbdesign.html" />
    
    
    <link rel="prev" href="../day16/data.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="6.2"
        data-chapter-title="SQL演练"
        data-filepath="day16/sql_code.md"
        data-basepath=".."
        data-revision="Wed Sep 26 2018 22:46:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day11/00.readme.html">
            
                
                    <a href="../day11/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python语法高级专题1
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day11/01.GIL.html">
            
                
                    <a href="../day11/01.GIL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        GIL问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day11/02.copy.html">
            
                
                    <a href="../day11/02.copy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        赋值-浅拷贝-深拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day11/03.import.html">
            
                
                    <a href="../day11/03.import.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day11/04.review.html">
            
                
                    <a href="../day11/04.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day12/00.readme.html">
            
                
                    <a href="../day12/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Python语法高级专题2
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day12/01.mro.html">
            
                
                    <a href="../day12/01.mro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        多继承和MRO顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="day12/02.property.html">
            
                
                    <a href="../day12/02.property.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        property属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day12/03.magic.html">
            
                
                    <a href="../day12/03.magic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        魔法属性和方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day12/04.with.html">
            
                
                    <a href="../day12/04.with.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        with管理上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day12/05.review.html">
            
                
                    <a href="../day12/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day13/00.readme.html">
            
                
                    <a href="../day13/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Python语法之装饰器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day13/01.closure.html">
            
                
                    <a href="../day13/01.closure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day13/02.decorator.html">
            
                
                    <a href="../day13/02.decorator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day13/03.review.html">
            
                
                    <a href="../day13/03.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day14/00.readme.html">
            
                
                    <a href="../day14/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        MySQL数据库基本操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day14/01.数据库概述.html">
            
                
                    <a href="../day14/01.数据库概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        数据库概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day14/02.数据类型和约束.html">
            
                
                    <a href="../day14/02.数据类型和约束.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据完整性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day14/03.Navicat.html">
            
                
                    <a href="../day14/03.Navicat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        图形化客户端Navicat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day14/04.命令行客户端.html">
            
                
                    <a href="../day14/04.命令行客户端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        命令行客户端SQL的使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="day14/05.sql_con.html">
            
                
                    <a href="../day14/05.sql_con.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        登录和退出数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="day14/07.sql_table_struct.html">
            
                
                    <a href="../day14/07.sql_table_struct.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        表结构的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="day14/06.sql_database.html">
            
                
                    <a href="../day14/06.sql_database.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        数据库的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="day14/08.表数据操作.html">
            
                
                    <a href="../day14/08.表数据操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        表数据的操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day14/09.review.html">
            
                
                    <a href="../day14/09.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        知识总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day15/00.readme.html">
            
                
                    <a href="../day15/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        MYSQL数据库进阶操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day15/01.练习.html">
            
                
                    <a href="../day15/01.练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        练习强化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day15/02.where条件.html">
            
                
                    <a href="../day15/02.where条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day15/03.排序查询.html">
            
                
                    <a href="../day15/03.排序查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="day15/04.分页查询.html">
            
                
                    <a href="../day15/04.分页查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="day15/05.聚合函数.html">
            
                
                    <a href="../day15/05.聚合函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        聚合函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="day15/06.分组.html">
            
                
                    <a href="../day15/06.分组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        分组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="day15/07.连接.html">
            
                
                    <a href="../day15/07.连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="day15/08.自连接.html">
            
                
                    <a href="../day15/08.自连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="day15/09.子查询.html">
            
                
                    <a href="../day15/09.子查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        子查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="day15/10.总结.html">
            
                
                    <a href="../day15/10.总结.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day16/readme.html">
            
                
                    <a href="../day16/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        MYSQL数据库编程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day16/data.html">
            
                
                    <a href="../day16/data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        数据准备
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.2" data-path="day16/sql_code.html">
            
                
                    <a href="../day16/sql_code.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        SQL演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day16/dbdesign.html">
            
                
                    <a href="../day16/dbdesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="day16/pymysql.html">
            
                
                    <a href="../day16/pymysql.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="day16/zongjie.html">
            
                
                    <a href="../day16/zongjie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="day17/00.readme.html">
            
                
                    <a href="../day17/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        MySQL数据库高级用法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="day17/01.view.html">
            
                
                    <a href="../day17/01.view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="day17/02.transaction.html">
            
                
                    <a href="../day17/02.transaction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        事务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="day17/03.sql_index.html">
            
                
                    <a href="../day17/03.sql_index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="day17/04.user.html">
            
                
                    <a href="../day17/04.user.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        用户管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="day17/05.master_slave.html">
            
                
                    <a href="../day17/05.master_slave.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        主从配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="day17/06.review.html">
            
                
                    <a href="../day17/06.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="day18/00.readme.html">
            
                
                    <a href="../day18/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        miniweb框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="day18/01.框架是什么.html">
            
                
                    <a href="../day18/01.框架是什么.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        框架概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="day18/02.实现框架.html">
            
                
                    <a href="../day18/02.实现框架.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        实现一个简单的框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="day18/03.模板.html">
            
                
                    <a href="../day18/03.模板.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        模板替换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="day18/04.路由.html">
            
                
                    <a href="../day18/04.路由.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        路由概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="day18/05.显示股票数据.html">
            
                
                    <a href="../day18/05.显示股票数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        股票收藏夹
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="day18/05.review.html">
            
                
                    <a href="../day18/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        复习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="2-sql&#x6F14;&#x7EC3;">2. SQL&#x6F14;&#x7EC3;</h1>
<h2 id="21-&#x5B66;&#x4E60;&#x76EE;&#x6807;">2.1 &#x5B66;&#x4E60;&#x76EE;&#x6807;</h2>
<ol>
<li>&#x77E5;&#x9053;&#x5916;&#x952E;&#x7EA6;&#x675F;&#x7684;&#x4F5C;&#x7528;</li>
</ol>
<hr>
<h2 id="22-sql&#x8BED;&#x53E5;&#x5F3A;&#x5316;">2.2 SQL&#x8BED;&#x53E5;&#x5F3A;&#x5316;</h2>
<ul>
<li>&#x67E5;&#x8BE2;&#x7C7B;&#x578B;cate_name&#x4E3A; &apos;&#x8D85;&#x6781;&#x672C;&apos; &#x7684;&#x5546;&#x54C1;&#x540D;&#x79F0;&#x3001;&#x4EF7;&#x683C;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> <span class="hljs-keyword">name</span>,price <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">where</span> cate_name = <span class="hljs-string">&apos;&#x8D85;&#x7EA7;&#x672C;&apos;</span>;</span>
</code></pre>
<ul>
<li>&#x663E;&#x793A;&#x5546;&#x54C1;&#x7684;&#x79CD;&#x7C7B;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> cate_name <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> cate_name;</span>
</code></pre>
<ul>
<li>&#x6C42;&#x6240;&#x6709;&#x7535;&#x8111;&#x4EA7;&#x54C1;&#x7684;&#x5E73;&#x5747;&#x4EF7;&#x683C;,&#x5E76;&#x4E14;&#x4FDD;&#x7559;&#x4E24;&#x4F4D;&#x5C0F;&#x6570;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> <span class="hljs-keyword">round</span>(<span class="hljs-keyword">avg</span>(price),<span class="hljs-number">2</span>) <span class="hljs-keyword">as</span> avg_price <span class="hljs-keyword">from</span> goods;</span>
</code></pre>
<ul>
<li>&#x663E;&#x793A;&#x6BCF;&#x79CD;&#x5546;&#x54C1;&#x7684;&#x5E73;&#x5747;&#x4EF7;&#x683C;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> cate_name,<span class="hljs-keyword">avg</span>(price) 
<span class="hljs-keyword">from</span> goods <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> cate_name;</span>
</code></pre>
<ul>
<li>&#x67E5;&#x8BE2;&#x6BCF;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x5546;&#x54C1;&#x4E2D; &#x6700;&#x8D35;&#x3001;&#x6700;&#x4FBF;&#x5B9C;&#x3001;&#x5E73;&#x5747;&#x4EF7;&#x3001;&#x6570;&#x91CF;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> cate_name,<span class="hljs-keyword">max</span>(price),<span class="hljs-keyword">min</span>(price),<span class="hljs-keyword">avg</span>(price),<span class="hljs-keyword">count</span>(*) 
<span class="hljs-keyword">from</span> goods <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> cate_name;</span>
</code></pre>
<ul>
<li>&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x4EF7;&#x683C;&#x5927;&#x4E8E;&#x5E73;&#x5747;&#x4EF7;&#x683C;&#x7684;&#x5546;&#x54C1;&#xFF0C;&#x5E76;&#x4E14;&#x6309;&#x4EF7;&#x683C;&#x964D;&#x5E8F;&#x6392;&#x5E8F;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> <span class="hljs-keyword">id</span>,<span class="hljs-keyword">name</span>,price <span class="hljs-keyword">from</span> goods 
<span class="hljs-keyword">where</span> price &gt; (<span class="hljs-keyword">select</span> <span class="hljs-keyword">round</span>(<span class="hljs-keyword">avg</span>(price),<span class="hljs-number">2</span>) <span class="hljs-keyword">as</span> avg_price <span class="hljs-keyword">from</span> goods) 
<span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> price <span class="hljs-keyword">desc</span>;</span>
</code></pre>
<h2 id="23-&#x6839;&#x636E;&#x5546;&#x54C1;&#x8868;&#x4E2D;&#x7684;&#x5206;&#x7C7B;&#x4FE1;&#x606F;&#x521B;&#x5EFA;&#x5206;&#x7C7B;&#x8868;">2.3 &#x6839;&#x636E;&#x5546;&#x54C1;&#x8868;&#x4E2D;&#x7684;&#x5206;&#x7C7B;&#x4FE1;&#x606F;&#x521B;&#x5EFA;&#x5206;&#x7C7B;&#x8868;</h2>
<h3 id="231-&#x521B;&#x5EFA;-&#x5546;&#x54C1;&#x5206;&#x7C7B;&#x8868;">2.3.1 &#x521B;&#x5EFA; &quot;&#x5546;&#x54C1;&#x5206;&#x7C7B;&quot;&#x8868;</h3>
<p>&#x7531;&#x4E8E;&#x53EA;&#x5B58;&#x5728;&#x4E00;&#x5F20;&#x8868; &#x5176;&#x4E2D;&#x65E2;&#x6709;&#x5546;&#x54C1;&#x4FE1;&#x606F;&#x53C8;&#x6709;&#x5206;&#x7C7B;&#x540D;&#x79F0;&#x548C;&#x54C1;&#x724C;&#x540D;&#x79F0;&#x3002;&#x5F53;&#x628A;&#x67D0;&#x79CD;&#x5206;&#x7C7B;&#x6216;&#x8005;&#x54C1;&#x724C;&#x7684;&#x5546;&#x54C1;&#x5168;&#x90E8;&#x5220;&#x9664;&#x540E;&#xFF0C;&#x672C;&#x4E0D;&#x5E94;&#x8BE5;&#x5220;&#x9664;&lt;&#x72EC;&#x7ACB;&#x4E8E;&#x5546;&#x54C1;&#x4FE1;&#x606F;&#x7684;&#x5206;&#x7C7B;&#x548C;&#x54C1;&#x724C;&gt;&#x7B49;&#x4FE1;&#x606F;&#x7ED3;&#x679C;&#x4E5F;&#x968F;&#x7740;&#x5220;&#x9664;&#x5546;&#x54C1;&#x4FE1;&#x606F;&#x800C;&#x5220;&#x9664;&#xFF0C;&#x90A3;&#x4E48;&#x6B64;&#x65F6;&#x5B58;&#x5728;<strong>&#x5220;&#x9664;&#x5F02;&#x5E38;</strong>&#x3002;</p>
<p>&#x4E3A;&#x89E3;&#x51B3;&#x5220;&#x9664;&#x5F02;&#x5E38;&#x95EE;&#x9898;&#xFF0C;&#x5219;&#x5FC5;&#x987B;&#x518D;&#x521B;&#x5EFA;&#x5206;&#x7C7B;&#x8868;&#x548C;&#x54C1;&#x724C;&#x8868;&#x5355;&#x72EC;&#x7528;&#x6765;&#x5B58;&#x50A8;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-sql"><span class="hljs-comment">-- &#x521B;&#x5EFA;&#x5546;&#x54C1;&#x5206;&#x7C7B;&#x8868;</span>
<span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> goods_cates(
    <span class="hljs-keyword">id</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> primary <span class="hljs-keyword">key</span> auto_increment,
    <span class="hljs-keyword">name</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">40</span>) <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span>
);</span>
</code></pre>
<h3 id="232-&#x5C06;&#x5206;&#x7C7B;&#x4FE1;&#x606F;&#x5BFC;&#x5165;&#x5230;&#x5206;&#x7C7B;&#x4E2D;">2.3.2 &#x5C06;&#x5206;&#x7C7B;&#x4FE1;&#x606F;&#x5BFC;&#x5165;&#x5230;&#x5206;&#x7C7B;&#x4E2D;</h3>
<ul>
<li>&#x67E5;&#x8BE2;goods&#x8868;&#x4E2D;&#x5546;&#x54C1;&#x7684;&#x79CD;&#x7C7B;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> cate_name <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> cate_name;</span>
</code></pre>
<ul>
<li>&#x5C06;&#x5206;&#x7EC4;&#x7ED3;&#x679C;&#x5199;&#x5165;&#x5230;goods_cates&#x6570;&#x636E;&#x8868;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> goods_cates (<span class="hljs-keyword">name</span>) 
    <span class="hljs-keyword">select</span> cate_name <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> cate_name;</span>
</code></pre>
<h3 id="233-&#x5C06;&#x5546;&#x54C1;&#x8868;&#x4E2D;&#x7684;&#x5206;&#x7C7B;&#x540D;&#x79F0;&#x66F4;&#x6539;&#x4E3A;&#x5206;&#x7C7B;&#x540D;&#x79F0;&#x5BF9;&#x5E94;&#x7684;&#x5206;&#x7C7B;id">2.3.3 &#x5C06;&#x5546;&#x54C1;&#x8868;&#x4E2D;&#x7684;&#x5206;&#x7C7B;&#x540D;&#x79F0;&#x66F4;&#x6539;&#x4E3A;&#x5206;&#x7C7B;&#x540D;&#x79F0;&#x5BF9;&#x5E94;&#x7684;&#x5206;&#x7C7B;id</h3>
<ul>
<li>&#x901A;&#x8FC7;goods_cates&#x6570;&#x636E;&#x8868;&#x6765;&#x66F4;&#x65B0;goods&#x8868;</li>
</ul>
<pre><code class="lang-sql"># <span class="hljs-operator"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">g</span> <span class="hljs-keyword">join</span> goods_cates gc <span class="hljs-keyword">on</span> <span class="hljs-keyword">g</span>.cate_name=gc.<span class="hljs-keyword">name</span> 

# &#x628A;&#x8BE5;&#x8BED;&#x53E5;&#x4E2D;<span class="hljs-keyword">from</span> &#x540E;&#x7684;&#x8BED;&#x53E5;&#x7406;&#x89E3;&#x4E3A;&#x4E00;&#x5F20;&#x865A;&#x8868;  

<span class="hljs-keyword">update</span> goods <span class="hljs-keyword">g</span>
    <span class="hljs-keyword">join</span> goods_cates gc
    <span class="hljs-keyword">on</span> <span class="hljs-keyword">g</span>.cate_name=gc.<span class="hljs-keyword">name</span> 
    <span class="hljs-keyword">set</span> <span class="hljs-keyword">g</span>.cate_name=gc.<span class="hljs-keyword">id</span>;</span>
</code></pre>
<h2 id="24-&#x6839;&#x636E;&#x5546;&#x54C1;&#x8868;&#x4E2D;&#x7684;&#x54C1;&#x724C;&#x4FE1;&#x606F;&#x521B;&#x5EFA;&#x54C1;&#x724C;&#x8868;">2.4 &#x6839;&#x636E;&#x5546;&#x54C1;&#x8868;&#x4E2D;&#x7684;&#x54C1;&#x724C;&#x4FE1;&#x606F;&#x521B;&#x5EFA;&#x54C1;&#x724C;&#x8868;</h2>
<h3 id="241-&#x521B;&#x5EFA;-&#x5546;&#x54C1;&#x54C1;&#x724C;&#x8868;-&#x8868;&#x5E76;&#x5BFC;&#x5165;&#x54C1;&#x724C;&#x540D;&#x79F0;">2.4.1 &#x521B;&#x5EFA; &quot;&#x5546;&#x54C1;&#x54C1;&#x724C;&#x8868;&quot; &#x8868;&#x5E76;&#x5BFC;&#x5165;&#x54C1;&#x724C;&#x540D;&#x79F0;</h3>
<ul>
<li>&#x901A;&#x8FC7;create table ...select&#x6765;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;&#x5E76;&#x4E14;&#x540C;&#x65F6;&#x5199;&#x5165;&#x8BB0;&#x5F55;,&#x4E00;&#x6B65;&#x5230;&#x4F4D;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-comment">-- select brand_name from goods group by brand_name;</span>

<span class="hljs-comment">-- &#x5728;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;&#x7684;&#x65F6;&#x5019;&#x4E00;&#x8D77;&#x63D2;&#x5165;&#x6570;&#x636E;</span>
<span class="hljs-comment">-- &#x6CE8;&#x610F;: &#x9700;&#x8981;&#x5BF9;brand_name &#x7528;as&#x8D77;&#x522B;&#x540D;&#xFF0C;&#x5426;&#x5219;name&#x5B57;&#x6BB5;&#x5C31;&#x6CA1;&#x6709;&#x503C;</span>
<span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> goods_brands (
    <span class="hljs-keyword">id</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> primary <span class="hljs-keyword">key</span> auto_increment,
    <span class="hljs-keyword">name</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">40</span>) <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span>) <span class="hljs-keyword">select</span> brand_name <span class="hljs-keyword">as</span> <span class="hljs-keyword">name</span> <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> brand_name;</span>
</code></pre>
<h3 id="242-&#x4F7F;&#x7528;&#x5546;&#x54C1;&#x54C1;&#x724C;&#x8868;&#x4E2D;&#x7684;&#x4FE1;&#x606F;&#x66F4;&#x65B0;&#x5546;&#x54C1;&#x8868;&#x4FE1;&#x606F;">2.4.2 &#x4F7F;&#x7528;&#x5546;&#x54C1;&#x54C1;&#x724C;&#x8868;&#x4E2D;&#x7684;&#x4FE1;&#x606F;&#x66F4;&#x65B0;&#x5546;&#x54C1;&#x8868;&#x4FE1;&#x606F;</h3>
<ul>
<li>&#x901A;&#x8FC7;goods_brands&#x6570;&#x636E;&#x8868;&#x6765;&#x66F4;&#x65B0;goods&#x6570;&#x636E;&#x8868;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">update</span> goods <span class="hljs-keyword">as</span> <span class="hljs-keyword">g</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> goods_brands <span class="hljs-keyword">as</span> b <span class="hljs-keyword">on</span> <span class="hljs-keyword">g</span>.brand_name=b.<span class="hljs-keyword">name</span> <span class="hljs-keyword">set</span> <span class="hljs-keyword">g</span>.brand_name=b.<span class="hljs-keyword">id</span>;</span>
</code></pre>
<h2 id="25-&#x4FEE;&#x6539;&#x5546;&#x54C1;&#x8868;&#x8868;&#x7ED3;&#x6784;">2.5 &#x4FEE;&#x6539;&#x5546;&#x54C1;&#x8868;&#x8868;&#x7ED3;&#x6784;</h2>
<ul>
<li>&#x67E5;&#x770B; goods &#x7684;&#x6570;&#x636E;&#x8868;&#x7ED3;&#x6784;,&#x4F1A;&#x53D1;&#x73B0; cate_name &#x548C; brand_name&#x5BF9;&#x5E94;&#x7684;&#x7C7B;&#x578B;&#x4E3A; <code>varchar</code> </li>
</ul>
<pre><code class="lang-sql">desc goods;
</code></pre>
<ul>
<li>&#x901A;&#x8FC7;alter table&#x8BED;&#x53E5;&#x4FEE;&#x6539;&#x8868;&#x7ED3;&#x6784;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> goods  
<span class="hljs-keyword">change</span> cate_name cate_id <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span>,
<span class="hljs-keyword">change</span> brand_name brand_id <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span>;</span>
</code></pre>
<h2 id="26-&#x5916;&#x952E;">2.6 &#x5916;&#x952E;</h2>
<blockquote>
<p>&#x5916;&#x952E;&#x8BF4;&#x660E;</p>
<pre><code>- foreign key&#x7EA6;&#x675F;&#x6307;&#x5B9A;&#x67D0;&#x4E00;&#x4E2A;&#x5217;&#x6216;&#x4E00;&#x7EC4;&#x5217;&#x4F5C;&#x4E3A;&#x5916;&#x90E8;&#x952E;&#xFF0C;&#x5176;&#x4E2D;&#x5305;&#x542B;&#x5916;&#x90E8;&#x952E;&#x7684;&#x8868;&#x79F0;&#x4E3A;&#x5B50;&#x8868;&#xFF0C;&#x5305;&#x542B;&#x5916;&#x952E;&#x6240;&#x5F15;&#x7528;&#x7684;&#x952E;&#x7684;&#x8868;&#x79F0;&#x4E3A;&#x7236;&#x8868;&#x3002;
</code></pre></blockquote>
<pre><code>- &#x5728;goods&#x8868;&#x4E2D;&#x62E5;&#x6709;cate_id&#x548C;brand_id&#x5206;&#x522B;&#x90FD;&#x6765;&#x81EA;&#x4E8E;goods_cates&#x8868;&#x548C;goods_brands&#x8868;
- cate_id&#x5B57;&#x6BB5;&#x7684;&#x503C;&#x5F15;&#x7528;&#x81EA;goods_cates&#x7684;id&#x5B57;&#x6BB5;&#xFF0C;&#x5373;cate_id&#x5C31;&#x662F;&quot;&#x5916;&#x952E;&quot;&#x3002;
- &#x4E00;&#x65E6;&#x6210;&#x4E3A;&#x771F;&#x6B63;&#x7684;&#x5916;&#x952E;&#xFF0C;&#x90A3;&#x4E48;&#x5916;&#x952E;&#x5C06;&#x4F1A;&#x5BF9;&#x7528;&#x6237;&#x4FEE;&#x6539;cate_id&#x4EA7;&#x751F;&#x7EA6;&#x675F;: cate_id &#x53D6;&#x503C;&#x8303;&#x56F4;&#x5C31;&#x662F; goods_cates&#x4E2D;id&#x5B57;&#x6BB5;&#x7684;&#x53D6;&#x503C;&#x8303;&#x56F4;,&#x5426;&#x5219;&#x5C06;&#x62A5;&#x9519;
- brand_id&#x540C;&#x7406; 
</code></pre><p>&#x4E0B;&#x9762;&#x54B1;&#x4EEC;&#x5F00;&#x59CB;&#x6765;&#x9A8C;&#x8BC1;&#x5916;&#x952E;&#x7684;&#x4F5C;&#x7528;</p>
<ul>
<li>&#x5206;&#x522B;&#x5728; goods_cates &#x548C; goods_brands&#x8868;&#x4E2D;&#x63D2;&#x5165;&#x8BB0;&#x5F55;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> goods_cates(<span class="hljs-keyword">name</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">&apos;&#x8DEF;&#x7531;&#x5668;&apos;</span>),(<span class="hljs-string">&apos;&#x4EA4;&#x6362;&#x673A;&apos;</span>),(<span class="hljs-string">&apos;&#x7F51;&#x5361;&apos;</span>);</span>
<span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> goods_brands(<span class="hljs-keyword">name</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">&apos;&#x6D77;&#x5C14;&apos;</span>),(<span class="hljs-string">&apos;&#x6E05;&#x534E;&#x540C;&#x65B9;&apos;</span>),(<span class="hljs-string">&apos;&#x795E;&#x821F;&apos;</span>);</span>
</code></pre>
<ul>
<li>&#x5728; goods &#x6570;&#x636E;&#x8868;&#x4E2D;&#x5199;&#x5165;&#x4EFB;&#x610F;&#x8BB0;&#x5F55;</li>
</ul>
<blockquote>
<p>&#x95EE;&#x9898;: SQL&#x8BED;&#x53E5;&#x4E2D;&#x7684;12&#x4EE3;&#x8868;&#x4EC0;&#x4E48;&#x610F;&#x4E49; ?
&#x6CA1;&#x9519; &#x662F;cate_id</p>
<p>&#x8BF7;&#x95EE;: goods_cates&#x8868;&#x4E2D;&#x6709;id=12&#x7684;&#x8BB0;&#x5F55;&#x5417;
&#x663E;&#x7136;&#x6CA1;&#x6709; </p>
</blockquote>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> goods (<span class="hljs-keyword">name</span>,cate_id,brand_id,price)
<span class="hljs-keyword">values</span>(<span class="hljs-string">&apos;LaserJet Pro P1606dn &#x9ED1;&#x767D;&#x6FC0;&#x5149;&#x6253;&#x5370;&#x673A;&apos;</span>, <span class="hljs-number">12</span>, <span class="hljs-number">4</span>,<span class="hljs-string">&apos;1849&apos;</span>);</span>
</code></pre>
<ul>
<li>&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x5546;&#x54C1;&#x7684;&#x8BE6;&#x7EC6;&#x4FE1;&#x606F; (&#x901A;&#x8FC7;&#x5185;&#x8FDE;&#x63A5;)</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> <span class="hljs-keyword">g</span>.<span class="hljs-keyword">id</span>,<span class="hljs-keyword">g</span>.<span class="hljs-keyword">name</span>,<span class="hljs-keyword">c</span>.<span class="hljs-keyword">name</span>,b.<span class="hljs-keyword">name</span>,<span class="hljs-keyword">g</span>.price <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">as</span> <span class="hljs-keyword">g</span>
<span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> goods_cates <span class="hljs-keyword">as</span> <span class="hljs-keyword">c</span> <span class="hljs-keyword">on</span> <span class="hljs-keyword">g</span>.cate_id=<span class="hljs-keyword">c</span>.<span class="hljs-keyword">id</span>
<span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> goods_brands <span class="hljs-keyword">as</span> b <span class="hljs-keyword">on</span> <span class="hljs-keyword">g</span>.brand_id=b.<span class="hljs-keyword">id</span>;</span>
</code></pre>
<ul>
<li>&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x5546;&#x54C1;&#x7684;&#x8BE6;&#x7EC6;&#x4FE1;&#x606F; (&#x901A;&#x8FC7;&#x5DE6;&#x8FDE;&#x63A5; &#x5C06;&#x5DE6;&#x8868;&#x672A;&#x663E;&#x793A;&#x6570;&#x636E;&#x6DFB;&#x52A0;&#x5230;&#x6700;&#x7EC8;&#x7ED3;&#x679C;)</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> <span class="hljs-keyword">g</span>.<span class="hljs-keyword">id</span>,<span class="hljs-keyword">g</span>.<span class="hljs-keyword">name</span>,<span class="hljs-keyword">c</span>.<span class="hljs-keyword">name</span>,b.<span class="hljs-keyword">name</span>,<span class="hljs-keyword">g</span>.price <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">as</span> <span class="hljs-keyword">g</span>
<span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> goods_cates <span class="hljs-keyword">as</span> <span class="hljs-keyword">c</span> <span class="hljs-keyword">on</span> <span class="hljs-keyword">g</span>.cate_id=<span class="hljs-keyword">c</span>.<span class="hljs-keyword">id</span>
<span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> goods_brands <span class="hljs-keyword">as</span> b <span class="hljs-keyword">on</span> <span class="hljs-keyword">g</span>.brand_id=b.<span class="hljs-keyword">id</span>;</span>
</code></pre>
<blockquote>
<p>&#x53D1;&#x73B0;&#x95EE;&#x9898;: cate_id = 12&#x7684;SQL&#x8BED;&#x53E5;&#x540D;&#x79F0;&#x63D2;&#x5165;&#x7684;&#x6570;&#x636E;&#x662F;&#x6709;&#x95EE;&#x9898;&#x7684;&#x3002;</p>
</blockquote>
<ul>
<li>&#x5982;&#x4F55;&#x9632;&#x6B62;&#x65E0;&#x6548;&#x4FE1;&#x606F;&#x7684;&#x63D2;&#x5165;,&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x5728;&#x63D2;&#x5165;&#x524D;&#x5224;&#x65AD;&#x7C7B;&#x578B;&#x6216;&#x8005;&#x54C1;&#x724C;&#x540D;&#x79F0;&#x662F;&#x5426;&#x5B58;&#x5728;&#x5462;? &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x8BB2;&#x8FC7;&#x7684;&#x5916;&#x952E;&#x6765;&#x89E3;&#x51B3;</li>
<li><p>&#x5916;&#x952E;&#x7EA6;&#x675F;:&#x5BF9;&#x5916;&#x952E;&#x5B57;&#x6BB5;&#x7684;&#x503C; &#x5728;&#x66F4;&#x65B0;&#x548C;&#x63D2;&#x5165;&#x65F6;&#x8FDB;&#x884C;&#x548C;&#x5F15;&#x7528;&#x7684;&#x8868;&#x4E2D;&#x5B57;&#x6BB5;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5BF9;&#x6BD4;</p>
</li>
<li><p>&#x5173;&#x952E;&#x5B57;: foreign key,&#x53EA;&#x6709; innodb&#x6570;&#x636E;&#x5E93;&#x5F15;&#x64CE; &#x652F;&#x6301;&#x5916;&#x952E;&#x7EA6;&#x675F;</p>
</li>
</ul>
<h3 id="261-&#x5BF9;&#x4E8E;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;">2.6.1 &#x5BF9;&#x4E8E;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;</h3>
<pre><code class="lang-sql"><span class="hljs-comment">-- &#x7ED9;brand_id &#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;&#x6210;&#x529F;</span>
<span class="hljs-operator"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> goods <span class="hljs-keyword">add</span> foreign <span class="hljs-keyword">key</span> (brand_id) <span class="hljs-keyword">references</span> goods_brands(<span class="hljs-keyword">id</span>);</span>

<span class="hljs-comment">-- &#x7ED9;cate_id &#x6DFB;&#x52A0;&#x5916;&#x952E;&#x5931;&#x8D25;</span>
<span class="hljs-comment">-- &#x4F1A;&#x51FA;&#x73B0;1452&#x9519;&#x8BEF;</span>
<span class="hljs-comment">-- &#x9519;&#x8BEF;&#x539F;&#x56E0;:&#x5DF2;&#x7ECF;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x4E0D;&#x5B58;&#x5728;&#x7684;cate_id&#x503C;12,&#x56E0;&#x6B64;&#x9700;&#x8981;&#x5148;&#x5220;&#x9664;</span>

<span class="hljs-operator"><span class="hljs-keyword">delete</span> <span class="hljs-keyword">from</span> goods <span class="hljs-keyword">where</span> cate_id = <span class="hljs-number">12</span>;</span>

<span class="hljs-operator"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> goods <span class="hljs-keyword">add</span> foreign <span class="hljs-keyword">key</span> (cate_id) <span class="hljs-keyword">references</span> goods_cates(<span class="hljs-keyword">id</span>);</span>
</code></pre>
<h3 id="262-&#x5728;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;&#x7684;&#x65F6;&#x5019;&#x8BBE;&#x7F6E;&#x5916;&#x952E;&#x7EA6;&#x675F;">2.6.2 &#x5728;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;&#x7684;&#x65F6;&#x5019;&#x8BBE;&#x7F6E;&#x5916;&#x952E;&#x7EA6;&#x675F;</h3>
<ul>
<li>&#x6CE8;&#x610F;: goods &#x4E2D;&#x7684; cate_id &#x7684;&#x7C7B;&#x578B;&#x4E00;&#x5B9A;&#x8981;&#x548C; goods_cates &#x8868;&#x4E2D;&#x7684; id &#x7C7B;&#x578B;&#x4E00;&#x81F4;</li>
</ul>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> goods(
    <span class="hljs-keyword">id</span> <span class="hljs-built_in">int</span> primary <span class="hljs-keyword">key</span> auto_increment <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span>,
    <span class="hljs-keyword">name</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">40</span>) <span class="hljs-keyword">default</span> <span class="hljs-string">&apos;&apos;</span>,
    price <span class="hljs-built_in">decimal</span>(<span class="hljs-number">5</span>,<span class="hljs-number">2</span>),
    cate_id <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span>,
    brand_id <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span>,
    is_show <span class="hljs-built_in">bit</span> <span class="hljs-keyword">default</span> <span class="hljs-number">1</span>,
    is_saleoff <span class="hljs-built_in">bit</span> <span class="hljs-keyword">default</span> <span class="hljs-number">0</span>,
    foreign <span class="hljs-keyword">key</span>(cate_id) <span class="hljs-keyword">references</span> goods_cates(<span class="hljs-keyword">id</span>),
    foreign <span class="hljs-keyword">key</span>(brand_id) <span class="hljs-keyword">references</span> goods_brands(<span class="hljs-keyword">id</span>)
);</span>
</code></pre>
<h3 id="263-&#x5220;&#x9664;&#x5916;&#x952E;&#x7EA6;&#x675F;">2.6.3 &#x5220;&#x9664;&#x5916;&#x952E;&#x7EA6;&#x675F;</h3>
<pre><code class="lang-sql"><span class="hljs-comment">-- &#x9700;&#x8981;&#x5148;&#x83B7;&#x53D6;&#x5916;&#x952E;&#x7EA6;&#x675F;&#x540D;&#x79F0;,&#x8BE5;&#x540D;&#x79F0;&#x7CFB;&#x7EDF;&#x4F1A;&#x81EA;&#x52A8;&#x751F;&#x6210;,&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x67E5;&#x770B;&#x8868;&#x521B;&#x5EFA;&#x8BED;&#x53E5;&#x6765;&#x83B7;&#x53D6;&#x540D;&#x79F0;</span>


<span class="hljs-operator"><span class="hljs-keyword">show</span> <span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> goods;</span>

<span class="hljs-comment">-- &#x83B7;&#x53D6;&#x540D;&#x79F0;&#x4E4B;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x540D;&#x79F0;&#x6765;&#x5220;&#x9664;&#x5916;&#x952E;&#x7EA6;&#x675F;</span>
<span class="hljs-operator"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> goods <span class="hljs-keyword">drop</span> foreign <span class="hljs-keyword">key</span> &#x5916;&#x952E;&#x540D;&#x79F0;;</span>
</code></pre>
<ul>
<li><strong>&#x4F7F;&#x7528;&#x5230;&#x5916;&#x952E;&#x7EA6;&#x675F;&#x4F1A;&#x6781;&#x5927;&#x7684;&#x964D;&#x4F4E;&#x8868;&#x66F4;&#x65B0;&#x7684;&#x6548;&#x7387;, &#x6240;&#x4EE5;&#x5728;&#x8FFD;&#x6C42;&#x8BFB;&#x5199;&#x6548;&#x7387;&#x4F18;&#x5148;&#x7684;&#x573A;&#x666F;&#x4E0B;&#x4E00;&#x822C;&#x5F88;&#x5C11;&#x4F7F;&#x7528;&#x5916;&#x952E;&#x3002;</strong></li>
</ul>
<h3 id="27-&#x603B;&#x7ED3;">2.7 &#x603B;&#x7ED3;</h3>
<ul>
<li><p>&#x5C06;&#x67E5;&#x8BE2;&#x7684;&#x6570;&#x636E;&#x76F4;&#x63A5;&#x63D2;&#x5165;&#x8868;&#x4E2D;</p>
<pre><code class="lang-sql">  <span class="hljs-operator"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> xxx (&#x5B57;&#x6BB5;&#x540D;) <span class="hljs-keyword">select</span> &#x8BED;&#x53E5;
  &#x5C06;<span class="hljs-keyword">select</span>&#x8BED;&#x53E5;&#x7684;&#x7ED3;&#x679C;&#x96C6;&#x63D2;&#x5165;&#x5230;&#x4E00;&#x4E2A;&#x8868;&#x4E2D;
</span></code></pre>
</li>
<li><p>&#x8FDE;&#x8868;&#x66F4;&#x65B0;</p>
<pre><code>update &#x8868;1 join &#x8868;2 on &#x8FDE;&#x63A5;&#x6761;&#x4EF6;
set &#x67D0;&#x8868;.&#x5B57;&#x6BB5;=&#x503C;
</code></pre></li>
<li><p>&#x5916;&#x952E;&#x7EA6;&#x675F;&#x4F5C;&#x7528; 
&#x5B50;&#x8868;&#x4E2D;&#x7684;&#x5916;&#x952E;&#x5B57;&#x6BB5;&#x5728;&#x63D2;&#x5165;&#x548C;&#x66F4;&#x65B0; &#x65B0;&#x503C;&#x7684;&#x65F6;&#x5019; &#x65B0;&#x503C;&#x5FC5;&#x987B; &#x5728;&#x4E3B;&#x8868;&#x4E2D;&#x76F8;&#x5E94;&#x5B57;&#x6BB5;&#x51FA;&#x73B0;&#x8FC7;&#x3002;</p>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day16/data.html" class="navigation navigation-prev " aria-label="Previous page: 数据准备"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day16/dbdesign.html" class="navigation navigation-next " aria-label="Next page: 数据库设计"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python高级 -->
</html>
